//
//  DownloadCell.m
//  Everest
//
//  Created by 李其昌 on 14/12/17.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "DownloadCell.h"
#define kCellWidth [UIApplication sharedApplication].keyWindow.bounds.size.width
#import "UIImageView+WebCache.h"
#import "FindHotAlbum.h"
#import "Download.h"
#import "AlbumTracks.h"
#import "MusicSearch.h"
#import "UIColor+AddColor.h"

@interface DownloadCell ()
@property (nonatomic, retain) UIImageView *myImageView;
@property (nonatomic, retain) UILabel *albumlabel;
@property (nonatomic, retain) UILabel *titleLabel;
//@property (nonatomic, retain) UIView *actionView;
@property (nonatomic, retain) UILabel *countLabel;
@property (nonatomic, retain) UILabel *numberLabel;
//@property (nonatomic, retain) UIButton *trashButton;
@property (nonatomic, retain) UILabel *lastupLabel;
@end

@implementation DownloadCell
- (void)dealloc
{
    [_hotAlbum release];
    [_myImageView release];
    [_albumlabel release];
    [_countLabel release];
    [_numberLabel release];
//    [_trashButton release];
    [_titleLabel release];
    [_lastupLabel release];
//    [_actionView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.myImageView];
//        self.myImageView.backgroundColor = [UIColor blueColor];
        [self.myImageView release];
        
        self.albumlabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.albumlabel];
//        self.albumlabel.backgroundColor = [UIColor redColor];
        [self.albumlabel release];
        
//        self.actionView = [[UIView alloc] init];
//        [self.contentView addSubview:self.actionView];
//        [self.actionView  release];
//        self.actionView.backgroundColor = [UIColor redColor];
        
        self.countLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.countLabel];
//        self.countLabel.backgroundColor = [UIColor blueColor];
        [self.countLabel release];
        
//        self.trashButton = [UIButton buttonWithType:UIButtonTypeSystem];
//        [self.contentView addSubview:self.trashButton];
//        self.trashButton.backgroundColor = [UIColor blueColor];
        
        self.numberLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.numberLabel];
//        self.numberLabel.backgroundColor = [UIColor blueColor];
        [self.numberLabel release];
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
//        self.titleLabel.backgroundColor = [UIColor blueColor];
        [self.titleLabel release];
        
        self.lastupLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.lastupLabel];
        [self.lastupLabel release];
//        self.lastupLabel.backgroundColor = [UIColor redColor];
    }
    return self;
}

- (void)setFindAlbum:(FindHotAlbum *)findAlbum
{
    if (_findAlbum != findAlbum) {
        [_findAlbum release];
        _findAlbum = [findAlbum retain];
    }
    self.titleLabel.text = self.findAlbum.title;
    self.numberLabel.text = [self.findAlbum.tracks description];
    [self.myImageView setImageWithURL:[NSURL URLWithString:self.findAlbum.coverSmall]];
    
    NSString *tempstr = [self.findAlbum.lastUptrackAt description];
    NSString *istr = [tempstr substringToIndex:10];
    int uptime = [istr intValue];
    NSDate *date = [NSDate date];
    NSTimeInterval time = [date timeIntervalSince1970];
    NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:- (time - uptime)];
    NSString *day = [NSString stringWithFormat:@"%@", date1];
    NSString *day1 = [day substringToIndex:10];
    self.lastupLabel.text = [NSString stringWithFormat:@"最后更新   %@", day1];
    
}

- (void)setTracks:(AlbumTracks *)tracks
{
    if (_tracks != tracks) {
        [_tracks release];
        
        _tracks = [tracks retain];
    }
    self.titleLabel.text = self.tracks.title;
    self.numberLabel.text = [self.tracks.tracks description];
    [self.myImageView setImageWithURL:[NSURL URLWithString:self.tracks.coverSmall]];
    
    NSDate *date = [NSDate date];
    NSTimeInterval time = [date timeIntervalSince1970];
    float lastUptrackAt = [self.tracks.updatedAt floatValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *timeDate = [NSDate dateWithTimeIntervalSinceNow:- (time - lastUptrackAt / 1000)];
    NSString *stime = @"最后更新:";
    NSString *timeStr =[stime stringByAppendingString:[formatter stringFromDate:timeDate]];
    self.lastupLabel.text = timeStr;
    
}

- (void)setMusic:(MusicSearch *)music
{
    if (_music != music) {
        [_music release];
        
        _music = [music retain];
    }
    self.titleLabel.text = self.music.title;
    self.numberLabel.text = [self.music.tracks description];
    [self.myImageView setImageWithURL:[NSURL URLWithString:self.music.cover_path]];
    
    NSDate *date = [NSDate date];
    NSTimeInterval time = [date timeIntervalSince1970];
    float lastUptrackAt = [self.music.created_at floatValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *timeDate = [NSDate dateWithTimeIntervalSinceNow:- (time - lastUptrackAt / 1000)];
    NSString *stime = @"最后更新:";
    NSString *timeStr =[stime stringByAppendingString:[formatter stringFromDate:timeDate]];
    self.lastupLabel.text = timeStr;
}

- (void)setHotAlbum:(FindHotAlbum *)hotAlbum
{
    if (_hotAlbum != hotAlbum) {
        [_hotAlbum release];
        _hotAlbum = [hotAlbum retain];
    }
    // 给 标题 赋值
    self.titleLabel.text = hotAlbum.title;
    // 给 节目数 赋值
    self.numberLabel.text = [self.hotAlbum.tracks description];
    // 给 照片 赋值
    [self.myImageView setImageWithURL:[NSURL URLWithString:self.hotAlbum.coverSmall]];
    // 给 更新时间 赋值
    NSString *tempstr = [self.hotAlbum.lastUptrackAt description];
    NSString *istr = [tempstr substringToIndex:10];
    int uptime = [istr intValue];
    NSDate *date = [NSDate date];
    NSTimeInterval time = [date timeIntervalSince1970];
    NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:- (time - uptime)];
    NSString *day = [NSString stringWithFormat:@"%@", date1];
    NSString *day1 = [day substringToIndex:10];
    self.lastupLabel.text = [NSString stringWithFormat:@"最后更新   %@", day1];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 图片位置
    self.myImageView.frame = CGRectMake(kCellWidth / 30, self.contentView.bounds.size.height / 10, kCellWidth / 7, kCellWidth / 7);
    // "辑"字位置
    self.albumlabel.frame = CGRectMake(kCellWidth / 30 + kCellWidth / 7 + kCellWidth / 375 * 10, self.contentView.bounds.size.height / 10, 20, 20);
    self.albumlabel.text = @"辑";
    self.albumlabel.textColor = [UIColor kuGouColor];
    self.albumlabel.font = [UIFont systemFontOfSize:17];
    
    // 标题位置
    self.titleLabel.frame = CGRectMake(kCellWidth / 30 + kCellWidth / 7 + 10 + 35, self.contentView.bounds.size.height / 10, kCellWidth / 2, 0);
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    // titleLabel自适应
    [self.titleLabel sizeToFit];
    
//    self.actionView.frame = CGRectMake(kCellWidth / 30 + kCellWidth / 7 + 10, self.titleLabel.frame.size.height + 25, self.contentView.bounds.size.width / 2, 15);
    
    // "节目数"位置
    self.countLabel.frame = CGRectMake(kCellWidth / 30 + kCellWidth / 7 + 10, self.titleLabel.frame.size.height + 15, 50, 15);
    self.countLabel.font = [UIFont systemFontOfSize:12];
    self.countLabel.text = @"节目数";
    self.countLabel.textColor = [UIColor grayColor];
    // number位置
    self.numberLabel.frame = CGRectMake(kCellWidth / 30 + kCellWidth / 7 + 10 + 10 + kCellWidth / 9 + 5, self.titleLabel.frame.size.height + 15, 30, 15);
    self.numberLabel.font = [UIFont systemFontOfSize:12];
//    self.numberLabel.text = @"1";
    self.numberLabel.textColor = [UIColor grayColor];
    
    // 最后更新时间
    self.lastupLabel.frame = CGRectMake((kCellWidth / 30 + kCellWidth / 7 + 10 + 10 + kCellWidth / 9 + 5) + 30, self.titleLabel.frame.size.height + 15, 150, 15);
//    self.lastupLabel.text = @"最后更新 2014年12月12日";
    self.lastupLabel.textColor = [UIColor grayColor];
    self.lastupLabel.font = [UIFont systemFontOfSize:12];
    
//    // 垃圾桶位置
//    self.trashButton.frame = CGRectMake(kCellWidth / 6 * 5, self.contentView.bounds.size.height / 667 * 240, 30, 30);
//    [self.trashButton setImage:[UIImage imageNamed:@"trash.png"] forState:UIControlStateNormal];
//    [self.trashButton addTarget:self action:@selector(trashButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    self.trashButton.tintColor = [UIColor blackColor];
    
}

- (void)trashButtonClicked:(UIButton *)button
{
    NSLog(@"清空");
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
